package com.registration.service;

import cn.dev33.satoken.stp.StpUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.registration.Annotation.OperateLog;
import com.registration.bean.DoctorBean;
import com.registration.bean.PCUserBean;
import com.registration.mapper.DoctorMapper;
import com.registration.mapper.PCUserMapper;
import com.registration.mapper.PCUserRoleMapper;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PCUserServiceImpl implements PCUserService{


    @Autowired
    private PCUserMapper pcUserMapper;
    @Autowired
    private PCUserRoleMapper pcUserRoleMapper;

    @Autowired
    private DoctorMapper doctorMapper;

    @Override
    //管理员信息
    public List<PCUserBean> adminInfo() {
        return pcUserMapper.adminInfo();
    }

    @Override
    @OperateLog("后台登录")
    //账号密码登录
    public PCUserBean login(String username, String password) {
        PCUserBean result;
        PCUserBean pcUserBean = pcUserMapper.login(username);
        if (pcUserBean == null) {
            throw new RuntimeException("该用户不存在");
        }
        if (!username.matches("^[a-zA-Z_]+$")) {

            throw new RuntimeException("用户名必须全英文");

        }
        if (pcUserBean.getStatus() == 0) {
            throw new RuntimeException("该用户已被禁用");
        }
        if (BCrypt.checkpw(password, pcUserBean.getPassword())) {
            result = pcUserBean;
            StpUtil.login(pcUserBean.getUsername());
            lastLoginTime(username);
        } else {
            result = null;
        }
        return result;
    }



    @Override
    public List<PCUserBean> getPCUserList() {
        return pcUserMapper.getPCUserList(new PCUserBean());
    }

    @Override
    @Transactional
    public int insert(PCUserBean pcUserBean) {
     String password=pcUserBean.getPassword();
     String encodePassword=BCrypt.hashpw(password,BCrypt.gensalt());
     String username= pcUserBean.getUsername();
     List<PCUserBean> pcUserBeans=pcUserMapper.getPCUserListByUsername(username);
     if(pcUserBeans.size()>0) {
         throw new RuntimeException("该用户已存在");
     }
     pcUserBean.setPassword(encodePassword);
        int res= pcUserMapper.insert(pcUserBean);
     int pc_user_id=pcUserBean.getPc_user_id();
     pcUserRoleMapper.insert(pc_user_id,pcUserBean.getRole_id());
     if(pcUserBean.getDept_id()!=null) {
         DoctorBean doctorBean = new DoctorBean();
         doctorBean.setDept_id(pcUserBean.getDept_id());
         doctorBean.setName(pcUserBean.getReal_name());
         doctorMapper.insert(doctorBean);
     }
    return res;
    }

    @Override
    @Transactional
    public int delete(Integer pc_user_id) {


        pcUserRoleMapper.deleteRoleByPCUserId(pc_user_id);
        PCUserBean pcUserBean= pcUserMapper.selectPCUserById(pc_user_id);
        doctorMapper.deleteByNameAndDeptId(pcUserBean.getReal_name(),pcUserBean.getDept_id());
        int res=pcUserMapper.delete(pc_user_id);
        return res;
    }

    @Override
    public int updatePCUser(PCUserBean pcUserBean) {

        int pc_user_id=pcUserBean.getPc_user_id();
        PCUserBean pcUserBean1= pcUserMapper.selectPCUserById(pc_user_id);
        int dept_id=pcUserBean1.getDept_id();
        String real_name=pcUserBean1.getReal_name();
        int doctor_id=doctorMapper.selectByNameAndDeptId(real_name,dept_id);
        int oldRoleId=pcUserRoleMapper.selectRoleIdByPCUserId(pc_user_id);
        int newRoleId=pcUserBean.getRole_id();
        if (oldRoleId!=newRoleId){
            pcUserRoleMapper.updateRoleByPCUserId(pc_user_id,newRoleId);
        }
        if(pcUserBean.getDept_id()==null){
            PCUserBean pcUserBean2= pcUserMapper.selectPCUserById(pc_user_id);
            doctorMapper.deleteByNameAndDeptId(pcUserBean2.getReal_name(),pcUserBean2.getDept_id());
        }else {
            DoctorBean doctorBean=new DoctorBean();
            doctorBean.setDoctor_id(doctor_id);
            doctorBean.setDept_id(pcUserBean.getDept_id());
            doctorBean.setName(pcUserBean.getReal_name());
            doctorBean.setStatus(pcUserBean.getStatus());
            doctorMapper.update(doctorBean);
        }
        String password=pcUserBean.getPassword();
        if(password!=null){
            String encodePassword=BCrypt.hashpw(password,BCrypt.gensalt());
            pcUserBean.setPassword(encodePassword);
        }

        int res=pcUserMapper.updatePCUser(pcUserBean);


        return res;
    }

    @Override
    public PageInfo<PCUserBean> getPCUserList(Integer pageNum, Integer pageSize, PCUserBean pcUserBean) {
        PageHelper.startPage(pageNum, pageSize);
        List<PCUserBean> pcUserBeans = pcUserMapper.getPCUserList(pcUserBean);
        PageInfo<PCUserBean> pageInfo = new PageInfo<>(pcUserBeans);

        return pageInfo;
    }


    @OperateLog("记录最后登陆时间")
    public void lastLoginTime(String username) {
        pcUserMapper.lastLoginTime(username);

    }
}
